package com.peng.appcommonlib;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;

import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.peng.commonlibrary.allkindsoflist.common.SampleHeader;
import com.peng.commonlibrary.allkindsoflist.expandablelist.ExpandSampleAdapter;
import com.peng.commonlibrary.allkindsoflist.expandablelist.ExpandSampleItem;
import com.peng.commonlibrary.allkindsoflist.expandablelist.ExpandableRecyclerAdapter;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    //private PreviewHandler mHandler = new PreviewHandler(this);

    /**
     * 服务器端一共多少条数据
     * 如果服务器没有返回总数据或者总页数，这里设置为最大值比如 10000，什么时候没有数据了根据接口返回判断
     */
    private static final int TOTAL_COUNTER = 34;

    /**
     * 每一页展示多少条数据
     */
    private static final int REQUEST_COUNT = 10;

    /**
     * 已经获取到多少条数据了
     */
    private static int mCurrentCounter = 0;

    private LRecyclerView mRecyclerView = null;
    private ExpandSampleAdapter mDataAdapter = null;
    private LRecyclerViewAdapter mLRecyclerViewAdapter = null;

    //private PreviewHandler mHandler = new PreviewHandler(this);

    private boolean isRefresh = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRecycleView();
    }

    private void initRecycleView() {
        mRecyclerView = (LRecyclerView) findViewById(R.id.list);

        // 初始化 Adapter
        mDataAdapter = new ExpandSampleAdapter(this, mRecyclerView);
        //设置模式，不可展开
        mDataAdapter.setMode(ExpandableRecyclerAdapter.MODE_ACCORDION);
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(mDataAdapter);
        mRecyclerView.setAdapter(mLRecyclerViewAdapter);

        // 设置布局的方式
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        // 必须添加头布局，点击事件的开始下标是从一开始
        mLRecyclerViewAdapter.addHeaderView(new SampleHeader(this));

        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.default_divider_height)
                .setPadding(R.dimen.default_divider_padding)
                .setColorResource(R.color.color_split_line)
                .build();

        // 当我们确定Item的改变不会影响RecyclerView的宽高的时候可以设置setHasFixedSize(true)
        // 并通过Adapter的增删改插方法去刷新RecyclerView，而不是通过notifyDataSetChanged()。
        // （其实可以直接设置为true，当需要改变宽高的时候就用 notifyDataSetChanged()去整体刷新一下）
        mRecyclerView.setHasFixedSize(true);

        // 设置 item 间的分割线
        mRecyclerView.addItemDecoration(divider);

        //删除item
//        mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
//            @Override
//            public void onItemClick(View view, int position) {
//                //mDataAdapter.remove(position);
//            }
//        });

        //禁用下拉刷新功能
        mRecyclerView.setPullRefreshEnabled(true);

        // 设置下拉刷新的进度提示样式
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallClipRotate);
        //设置下拉刷新的时显示的箭头
        mRecyclerView.setArrowImageView(R.drawable.ic_pulltorefresh_arrow);
        // 设置加载更多的进度提示样式
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);

        mRecyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                mLRecyclerViewAdapter.notifyDataSetChanged();//fix bug:crapped or attached views may not be recycled. isScrap:false isAttached:true
                mCurrentCounter = 0;
                mRecyclerView.refreshComplete(10);
            }
        });

        //禁用自动加载更多功能
        mRecyclerView.setLoadMoreEnabled(true);
        mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {

                if (mCurrentCounter < TOTAL_COUNTER) {
                    // 加载更多
                    //requestData();
                } else {
                    //已经加载完全部数据
                    mRecyclerView.setNoMore(true);
                }
            }
        });

        // 滚动监听
        mRecyclerView.setLScrollListener(new LRecyclerView.LScrollListener() {

            @Override
            public void onScrollUp() {
            }

            @Override
            public void onScrollDown() {
            }


            @Override
            public void onScrolled(int distanceX, int distanceY) {
            }

            @Override
            public void onScrollStateChanged(int state) {

            }

        });

        //设置头部加载颜色
        mRecyclerView.setHeaderViewColor(R.color.colorAccent, R.color.colorPrimary, android.R.color.white);
        //设置底部加载颜色
        mRecyclerView.setFooterViewColor(R.color.colorAccent, R.color.colorPrimary, android.R.color.white);
        //设置底部加载文字提示
        mRecyclerView.setFooterViewHint("拼命加载中", "已经全部为你呈现了", "网络不给力啊，点击再试一次吧");

        addItems(mDataAdapter.getSampleItems());
    }


    private void notifyDataSetChanged() {
        mLRecyclerViewAdapter.notifyDataSetChanged();
    }

    private void addItems(ArrayList<ExpandSampleItem> list) {

        mDataAdapter.setItems(list);
        mCurrentCounter += list.size();
    }

}
